package com.xiaoxiaoyu.framework.mvc;

import com.xiaoxiaoyu.framework.aop.AspectWeaver;
import com.xiaoxiaoyu.framework.core.BeanContainer;
import com.xiaoxiaoyu.framework.inject.DependentInject;
import com.xiaoxiaoyu.framework.mvc.processor.RequestProcessor;
import com.xiaoxiaoyu.framework.mvc.processor.impl.ControllerRequestProcessor;
import com.xiaoxiaoyu.framework.mvc.processor.impl.JspRequestProcessor;
import com.xiaoxiaoyu.framework.mvc.processor.impl.PreRequestProcessor;
import com.xiaoxiaoyu.framework.mvc.processor.impl.StaticResourceRequestProcessor;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

/**
 * @program: simple-Spring
 * @description:
 * @author: 小小鱼
 * @date: 2021-02-06 21:49
 **/
public class DispatcherServlet extends HttpServlet {
    List<RequestProcessor> PROCESSOR = new ArrayList<>();

    @Override
    public void init(){
        //1.初始化容器
        BeanContainer beanContainer = BeanContainer.getInstance();
        String pakeName="";
        beanContainer.loaderBeans(pakeName);
        new AspectWeaver().doAop();
        new DependentInject().doIoc();
        //2.初始化请求处理器责任链
        PROCESSOR.add(new PreRequestProcessor());
        PROCESSOR.add(new StaticResourceRequestProcessor(getServletContext()));
        PROCESSOR.add(new JspRequestProcessor(getServletContext()));
        PROCESSOR.add(new ControllerRequestProcessor());
    }

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) {
        //1.创建责任链对象实例
        RequestProcessorChain requestProcessorChain = new RequestProcessorChain(PROCESSOR.iterator(), req, resp);
        //2.通过责任链模式来依次调用请求处理器对请求进行处理
        requestProcessorChain.doRequestProcessorChain();
        //3.对处理结果进行渲染
        requestProcessorChain.doRender();
    }




}
